import re

import json

import sys


def match_fix(data):


try:

json.loads(data);

return data;

except json.JSONDecodeError as e:

print('fix data: ' + data)

fixResult = start_fix(data.replace(" ", ""));

json.loads(fixResult);

return fixResult;


def start_fix(data):


reg = r'(fields":\[.*?)((,"period)|(,"start_date)|(,"stop_date))';

result = re.sub(reg, r'\1]\2', data)

print('fix result: ' + result);

return result;

# 示例数据

data1 = '{"org_info": "中国银行", "fields": ["长期借款", "period": "年", "start_date": "20231018", "stop_date": "20231018"}'

data2 = '{"org_info": "中国银行", "fields": ["长期借款", "应付债券总额", "period": "年", "start_date": "20231018", "stop_date": "20231018"}'

data3 = '{"org_info": "中国银行", "fields": ["长期借款","应付债券总额"], "period": "年", "start_date": "20231018", "stop_date": "20231018"}'

data4 = '{"org_info": "中国银行", "fields": ["长期借款", "start_date": "20231018", "period": "年", "stop_date": "20231018"}'

data5 = '{"org_info": "中国银行", "fields": ["长期借款", "应付债券总额", "stop_date": "20231018", "period": "年", "start_date": "20231018"}'

data6 = '{"org_info": "中国银行", "fields": ["长期借款","应付债券总额"], "period": "年", "start_date": "20231018", "stop_date": "20231018"}'

# 修复 "fields"

fixed_data1 = match_fix(data1)

fixed_data2 = match_fix(data2)

fixed_data3 = match_fix(data3)

fixed_data4 = match_fix(data4)

fixed_data5 = match_fix(data5)

fixed_data6 = match_fix(data6)

print("==============================")

# 打印修复后的数据

print(fixed_data1)

print(fixed_data2)

print(fixed_data3)

print(fixed_data4)

print(fixed_data5)

print(fixed_data6)
